package com.shigu.component.shiro;

import com.opentae.data.mall.beans.RolePermission;
import com.opentae.data.mall.interfaces.ManagerPermissionMapper;
import com.opentae.data.mall.interfaces.ManagerRolePermissionMapper;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 创建角色权限对应类
 * Created by zhaohongbo on 16/5/27.
 */
@Repository
public class SimpleRolePermissionReader {

    private static Map<String,Long> relationTime=new HashMap<>();
    private static Map<String,Collection<Permission>> rolePermissions=new HashMap<>();

    @Autowired
    ManagerRolePermissionMapper managerRolePermissionMapper;

    @Autowired
    ManagerPermissionMapper managerPermissionMapper;
    /**
     * 返回角色权限对应关系
     * @param s
     * @return
     */
    public Collection<Permission> resolvePermissionsInRole(String s) {

        //拿到这个角色最新的权限时间
        Long lastTime=managerRolePermissionMapper.selLastModifyByRoleTag(s);
        Long hereTime=relationTime.get(s);
        Collection<Permission> list=null;
        if(hereTime==null||!hereTime.equals(lastTime)){//拿动态
            list=makeActivePermission(s);
            rolePermissions.put(s,list);
            relationTime.put(s,lastTime);
        }else{//拿静态
            list=rolePermissions.get(s);
        }
        return list;
    }

    /**
     * 制作动态权限
     * @param s
     * @return
     */
    public Collection<Permission> makeActivePermission(String s){
        List<RolePermission> permissions=managerPermissionMapper.selPermissiontagsByroleTag(s);
        List<Permission> list=new ArrayList<Permission>();
        for(RolePermission rp:permissions){
            list.add(new WildcardPermission(rp.getPermissionTag()));
        }
        return list;
    }
}
